package cn.icare.cms.impl;

import cn.icare.cms.OrderPayingService;
import cn.icare.cms.StringUtil;
import cn.icare.cms.dao.mapper.OrderPayingMapper;
import cn.icare.cms.dao.po.OrderPaying;
import cn.icare.cms.dao.po.Position;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderPayingServiceImpl implements OrderPayingService {

    @Resource
    private OrderPayingMapper orderPayingMapper;

    @Override
    public boolean saveOne(OrderPaying ro) {

        boolean exists = orderPayingMapper.existsWithPrimaryKey(ro.getOrderid());
        ro.setCreatetime(new Date());
        int result = 0;
        if (exists) {
            result = this.orderPayingMapper.updateByPrimaryKeySelective(ro);
        } else {

            result = this.orderPayingMapper.insertSelective(ro);
        }
        return result > 0;
    }

    @Override
    public boolean deleteOne(Long orderId) {

        return this.orderPayingMapper.deleteByPrimaryKey(orderId) > 0;
    }

    @Override
    public MsgDatasVO getList(Integer pageNum, Integer pageSize) {

        MsgDatasVO result = new MsgDatasVO();
        List<OrderPaying> dataList;
        if (pageSize == 0) {
            //不分页，获取全部数据
            dataList = this.orderPayingMapper.selectAll();
            result.setTotal(dataList.size());
        } else {
            PageHelper.startPage(pageNum, pageSize);
            dataList = this.orderPayingMapper.selectAll();
            PageInfo pageInfo = new PageInfo(dataList);
            result.setTotal(pageInfo.getTotal());

        }
        result.setDatas(dataList);
        return result;
    }
}
